package com.jackrain.nea.fc.dl.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Mapper
public interface ViewDLPurOrderToF4Mapper {

    @SelectProvider(type = ViewDLPurOrderSql.class, method = "findOrderData")
    List<HashMap> selOrderData(@Param("id") Object[] id,
                               @Param("status") Integer status,
                               @Param("isactive") String isactive);

    @SelectProvider(type = ViewDLPurOrderSql.class, method = "findOrderItemData")
    List<HashMap> selOrderItemData(@Param("id") Long id);

    @Select("SELECT ECODE FROM DL_C_BILLDIM_ITEM WHERE ID=#{id}")
    String selPurReasonEcode(Long id);

    class ViewDLPurOrderSql {

        public String findOrderData(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            Integer status = (Integer) map.get("status");
            String isactive = (String) map.get("isactive");
            return new SQL() {
                {
                    SELECT("ID,BILL_NO,BILL_DATE,FROM_BILL_NO,CP_C_SUPPLIER_ECODE,CP_C_STORE_ECODE,REMARK," +
                            "CREATIONDATE,OWNERENAME,SUM_QTY_BILL,SUM_AMT_ACTUAL,BILL_TYPE,PLAN_STORES," +
                            "ESTIMATED_DELIVERY_DATE,DATE_IN,PUR_REASON,MODIFIERNAME,MODIFIERENAME,MODIFIEDDATE," +
                            "DL_B_PUR_REQ_NO,ORDER_PHASE,DL_B_PUR_SETTLEMENT_ID,APPROVAL_STATUS,APPROVALER_ENAME,APPROVAL_TIME,PARENT_BILL_TYPE,TERMINATE_TYPE");
                    FROM("DL_B_PUR_ORDER");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    if (null != status) {
                        WHERE("STATUS = " + status);
                    }
                    if (null != isactive) {
                        WHERE("ISACTIVE = '" + isactive + "'");
                    }
                }
            }.toString();
        }

        public String findOrderItemData(Map<String, Object> map) {
            Long id = (Long) map.get("id");
            return new SQL() {
                {
                    SELECT("PS_C_SKU_ECODE,QTY_BILL,PRICE_ACTUAL,DPRICE_LIST,RETAIL_RATE,OVER_ADD_QTY,PRICE_MGT,AMT_MGT,PRICE_NON_TAX,AMT_NON_TAX");
                    FROM("DL_B_PUR_ORDER_ITEM");
                    WHERE("DL_B_PUR_ORDER_ID =" + id);
                }
            }.toString();
        }

    }


}
